﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using NymphsLib.CommonUtilities.FileSystem;
using MovieBrowser.Controller;
using MovieBrowser.Event;
using MovieBrowser.Model;
using MovieBrowser.Presentation.Events;

namespace MovieBrowser.Presentation.Forms.Dialogs
{
    public partial class CopyQueueForm : Form, IDebugLogEvent
    {
        public event EventHandler DebugText;
        readonly FolderBrowserDialog _folderBrowserDialog = new FolderBrowserDialog();

        public void InvokeOnDebugText(Exception exception)
        {
            var handler = DebugText;
            if (handler != null) handler(this, new DebugTextEventArgs(exception));
        }
        public void InvokeOnDebugText(string text)
        {
            var handler = DebugText;
            if (handler != null) handler(this, new DebugTextEventArgs(text));
        }

        public CopyQueueForm()
        {
            InitializeComponent();

            olvColumnSize.AspectToStringConverter = delegate(object x)
            { return (long)x < 0 ? "" : FileHelper.FormatFileSize((long)x); };
        }

        public void AddToQueue(IEnumerable<Movie> movies)
        {
            foreach (var movie in movies)
            {
                try
                {
                    var newMovie = Movie.Clone(movie);
                    newMovie.Size = FileHelper.FolderSize(newMovie.FilePath);
                    newMovie.MovieChecked = true;


                    bool found = objectListView1.Objects != null && objectListView1.Objects.Cast<Movie>().Any(m => m.ImdbId == newMovie.ImdbId);
                    if (!found)
                        objectListView1.AddObject(newMovie);

                }
                catch (Exception exception)
                {
                    InvokeOnDebugText(exception);
                }
            }

            CalculateTotalSize();
        }

        private void CalculateTotalSize()
        {
            var size = objectListView1.CheckedObjects.Cast<Movie>().Sum(movie => movie.Size);
            labelSize.Text = FileHelper.FormatFileSize(size);
        }

        private void Button1Click(object sender, EventArgs e)
        {
            if (_folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                labelPath.Text = _folderBrowserDialog.SelectedPath;
            }
        }

        private void ObjectListView1ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            CalculateTotalSize();
        }

        private void CommandHideQueueClick(object sender, EventArgs e)
        {
            Hide();
        }

        private void ObjectListView1KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                foreach (Movie o in objectListView1.SelectedObjects)
                {
                    o.MovieChecked = false;
                    objectListView1.RemoveObject(o);
                }
                CalculateTotalSize();
            }
        }

        private void CommandCopyQueueClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(labelPath.Text))
            {

                OverwritePolicy policy = OverwritePolicy.Skip;
                policy = radioAsk.Checked ? OverwritePolicy.Ask : policy;
                policy = radioOverwrite.Checked ? OverwritePolicy.Overwrite : policy;

                MovieBrowserController.SendTo(objectListView1.CheckedObjects.Cast<Movie>(), labelPath.Text, policy, checkBoxMove.Checked);

                foreach (Movie movie in objectListView1.CheckedObjects)
                {
                    movie.MovieChecked = false;
                    objectListView1.RemoveObject(movie);
                }
                CalculateTotalSize();
            }
            else
            {
                MessageBox.Show(@"No destination selected.");
            }

        }

        private void commandMoveQueue_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(labelPath.Text))
            {
                MovieBrowserController.MoveTo(objectListView1.CheckedObjects.Cast<Movie>(), labelPath.Text, checkBoxMove.Checked);

                foreach (Movie movie in objectListView1.CheckedObjects)
                {
                    movie.MovieChecked = false;
                    objectListView1.RemoveObject(movie);
                }
                CalculateTotalSize();
            }
            else
            {
                MessageBox.Show(@"No destination selected.");
            }
        }


    }
}
